package com.example.demo4.controller;

import com.example.demo4.dao.CityDao;
import com.example.demo4.entity.City;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.UUID;

import org.springframework.web.bind.annotation.GetMapping;

@RestController
@Slf4j
@RequestMapping("city")
public class CityApi {

    @Autowired
    CityDao cityDao;

    @GetMapping("list")
    public List<City> getCities(){
        return cityDao.findAll();
    }

    @PostMapping
    public City add(@RequestBody City city){
        city.setId(UUID.randomUUID().toString());
        return cityDao.save(city);
    }

    @PutMapping
    public City update(@RequestBody City city){
        return cityDao.save(city);
    }
    @DeleteMapping("{id}")
    public void delete(@PathVariable String id){
        cityDao.deleteById(id);
    }

    @GetMapping("id/{id}")
    public City getById(@PathVariable String id){
        return cityDao.findById(id).get();
    }

}
